﻿<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../JS/jquery.js"></script>
<script src="../JS/getcookie.js" ></script>
<script>

	var postOK = false;
	var nameOK = false;
	var passwordOK = false;
	var repasswordOK = false;
	var emailOK = false;
	var phoneOK = false;
	var birthdayOK = false;

	function chkname(){
		var nameStr = $("#name")[0].value;
		var nameLen = nameStr.length;
		if( nameLen<3 || nameLen>10 )
		{
			$("#nametip").html("<em style='color:red'>用户名长度应该为3到10间</em>");
			nameOK = false;
		}
		else
		{
			postOK = false;
			$.post("../PHP/chkname.php",{ name:nameStr},
			function(data,status){
				if(data == "ok")
				{
					$("#nametip").html("<em style='color:green'>用户名可用</em>");
					nameOK = true;
				}
				else if(data == "has")
				{
					$("#nametip").html("<em style='color:red'>用户名被占用</em>");
					nameOK = false;
				}
				else
				{
					$("#nametip").html("<em style='color:red'>别开玩笑了</em>");
					nameOK = false;
				}
				postOK = true;
			});
		}
	};

	function chkpost() {
		if( postOK )
		{
			if( nameOK && passwordOK && repasswordOK && emailOK && phoneOK && birthdayOK )
			{
				$("#registerBtn")[0].disabled = true;
				$("#registerBtn")[0].value = "正在注册……" ;
				var nameStr = $("#name")[0].value;
				var passwordStr = $("#password")[0].value;
				var repasswordStr = $("#repassword")[0].value;
				var emailStr = $("#email")[0].value;
				var phoneStr = $("#phone")[0].value;
				var birthdayStr = $("#birthday")[0].value;
				var sexStr = $("#sex")[0].value;
				
				$.post("../PHP/adduser.php",
				{
					name:		nameStr,
					password:	passwordStr,
					email:		emailStr,
					phone:		phoneStr,
					birthday:	birthdayStr,
					sex:		sexStr
				},
				function(data,status){
					if( data == "ok" )
					{
						$("#registerBtn")[0].value = "正在发送激活邮件……";
						$.post("../PHP/sendemail.php?name="+nameStr,
						{
							password:	passwordStr,
							email:		emailStr
						},
						function(data,status) {
							setTimeout("parent.location = 'login.htm'",1000);
							delCookie("name");
							$("#registerBtn")[0].value = "邮件发送完成，正在为您跳转……";
						});
					}
					else
					{
						alert("注册失败，错误信息为\n"+data);
						$("#registerBtn")[0].disabled = false;
						$("#registerBtn")[0].value = "注册" ;
					}
				});
			}
			else
			{
				alert("请检查书写~");
				var str = "error:";
				if( !nameOK ) str+="nameOK";
				if( !passwordOK ) str+="passwordOK";
				if( !repasswordOK ) str+="repasswordOK";
				if( !emailOK ) str+="emailOK";
				if( !phoneOK ) str+="phoneOK";
				if( !birthdayOK ) str+="birthdayOK";
			}
		}
		else
		{
			setTimeout("chkpost()",100);
		}
	};

	function chkpassword(){
		var passwordStr = $("#password")[0].value;
		var passwordLen = passwordStr.length;
		if( passwordLen<6 || passwordLen>16)
		{
			$("#passwordtip").html("<em style='color:red'>密码长度应该为6到16间</em>");
			passwordOK = false;
		}
		else
		{
			$("#passwordtip").html("<em style='color:green'>密码符合条件</em>");
			passwordOK = true;
		}
	};
	
	function chkrepassword(){
		var passwordStr = $("#password")[0].value;
		var repasswordStr = $("#repassword")[0].value;
		
		if( passwordOK )
		{
			if( passwordStr == repasswordStr )
			{
				$("#repasswordtip").html("<em style='color:green'>两次密码一致</em>");
				repasswordOK = true;
			}
			else
			{
				$("#repasswordtip").html("<em style='color:red'>两次密码不一致</em>");
				repasswordOK = false;
			}
		}
	};
	
	function chkemail(){
		var emailStr = $("#email")[0].value;
		if( emailStr.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) == null )
		{
			$("#emailtip").html("<em style='color:red'>email格式错误</em>");
			emailOK = false;
		}
		else
		{
			$("#emailtip").html("<em style='color:green'>email格式正确</em>");
			emailOK = true;
		}
	};
	
	function chkphone(){
		var phoneStr = $("#phone")[0].value;
		var phoneLen = phoneStr.length;
		if( phoneStr.match("^6[0-9]*$") == null || parseInt((phoneLen+1)/2) != 3)
		{
			$("#phonetip").html("<em style='color:red'>短号格式错误</em>");
			phoneOK = false;
		}
		else
		{
			$("#phonetip").html("<em style='color:green'>短号格式正确</em>");
			phoneOK = true;
		}
	};
	
	function chkbirthday(){
		var birthdayStr = $("#birthday")[0].value;
		var birthdayLen = birthdayStr.length;
		
		if( birthdayStr.match("19[8-9][0-9][0-1][0-9][0-9][0-9]$") == null )
		{
			$("#birthdaytip").html("<em style='color:red'>生日格式错误</em>");
			birthdayOK = false;
		}
		else
		{
			$("#birthdaytip").html("<em style='color:green'>生日格式正确</em>");
			birthdayOK = true;
		}
	};

$(document).ready(function(){
	
	// initial
	$("div").css("display","inline-block");
	$("p").css("display","inline-block");
	$("form").css("display","inline-block");

	$("#name").blur( chkname );
	$("#password").blur( chkpassword );
	$("#repassword").blur( chkrepassword );
	$("#email").blur( chkemail );
	$("#phone").blur( chkphone );
	$("#birthday").blur( chkbirthday );
	
	$("#registerBtn")[0].disabled = false;
	$("#registerBtn")[0].value = "注册" ;

	$("#registerBtn").click(function(){
		chkname();
		chkpassword();
		chkrepassword();
		chkemail();
		chkbirthday();
		chkphone();
		chkpost();
	});
});
</script> 
</head>
 
<body>

<div>
	<p>注册名称：</p>
	<input type="text" id="name" />
	<div id="nametip"></div>
</div> <br/>

<div>
	<p>注册密码：</p>
	<input type="password" id="password"/>
	<div id="passwordtip"></div>
</div> <br/>

<div>
	<p>确认密码：</p>
	<input type="password" id="repassword"/>
	<div id="repasswordtip"></div>
</div> <br/>

<div>
	<p>电子邮箱：</p>
	<input type="text" id="email"/>
	<div id="emailtip"></div>
</div> <br/>

<div>
	<p>华工短号：</p>
	<input type="text" id="phone"/>
	<div id="phonetip"></div>
</div> <br/>

<div>
	<p>你的生日：</p>
	<input type="text" id="birthday"/>
	<div id="birthdaytip"></div>
</div> <br/>

<div>
	<p>你的性别：</p>
	<form>
		<select id="sex" style="width:155px">
		<option value="man">男</option>
		<option value="woman">女</option>
	</select>
	<form>
</div> <br/>

<div>
	<input type="button" value="注册" id="registerBtn" />
	<div id="output"></div>
	<a name = "bottom" />
</div>
</body>
</html>